package com.ruibang.glass.sale.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.ruibang.glass.common.constants.CommonErrorCode;
import com.ruibang.glass.common.domain.GlobalUser;
import com.ruibang.glass.common.domain.camunda.BpmProcessInstanceCreate;
import com.ruibang.glass.sale.constant.SaleErrorCode;
import com.ruibang.glass.sale.feign.CamundaApi;
import com.teaming.cloud.framework2.autoconfigure.redis.RedisSingleService;
import com.teaming.cloud.framework2.common.exception.ServiceException;
import com.teaming.cloud.framework2.common.model.ResultBody;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Optional;

@Slf4j
@Service
public class CommonService {


    @Autowired
    private CamundaApi camundaApi;

    @Autowired
    private RedisSingleService rediservice;




    /***
     * 发起流程
     * @param bpmProcessInstanceCreate
     * @return
     */
    public String startProcess(BpmProcessInstanceCreate bpmProcessInstanceCreate){
        if(StringUtils.isNotBlank(bpmProcessInstanceCreate.getMenuId())){
            ResultBody<String> processRs = camundaApi.createProcessInstance(bpmProcessInstanceCreate,getToken());
            if(processRs!=null&& StringUtils.isNotBlank(processRs.getData())){
                return processRs.getData();
            }
            throw new ServiceException(CommonErrorCode.PROCESS_START_FAILED.getCode(), CommonErrorCode.PROCESS_START_FAILED.getMsg() );
        }else{
            throw new ServiceException(SaleErrorCode.BUSINESS_MENU_IS_EMPTY.getCode(), SaleErrorCode.BUSINESS_MENU_IS_EMPTY.getMsg() );
        }
    }



    /**
     * 获取userId
     *
     * @return
     */
    public String getToken() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader("token");
        if (token == null) {
            token = String.valueOf(request.getAttribute("token"));
        }
        return token;
    }

    /**
     * 获取userId
     *
     * @return
     */
    public GlobalUser getUser() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        Object token = request.getHeader("token");
        if (token == null) {
            token = request.getAttribute("token");
        }
        if (Optional.ofNullable(token).isPresent()) {
            if (rediservice.hasKey(token.toString())) {
                String json = rediservice.get(token.toString()).toString();
                if (StringUtils.isNotBlank(json)) {
                    return JSON.parseObject(json, GlobalUser.class);
                }
            }
        } else {
            throw new ServiceException(CommonErrorCode.USER_NO_LOGIN_OR_INVALID.getCode(), CommonErrorCode.USER_NO_LOGIN_OR_INVALID.getMsg());
        }
        return null;
    }
}
